<template>
	<div class="system-dept-container">
		<el-card shadow="hover">
			<div class="system-dept-search mb15">
				<el-input size="default" placeholder="请输入数据名称" style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" class="ml10" @click="onOpenAddCrawl">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增爬虫规则
				</el-button>
			</div>
			<el-table
				:data="tableData.data"
				style="width: 100%"
				row-key="id"
				default-expand-all
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
			>
				<el-table-column prop="crawlName" label="数据名称" show-overflow-tooltip> </el-table-column>
				<el-table-column label="排序" show-overflow-tooltip width="80">
					<template #default="scope">
						{{ scope.$index }}
					</template>
				</el-table-column>
				<el-table-column prop="status" label="爬虫状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="describe" label="爬虫描述" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="140">
					<template #default="scope">
						<el-button size="small" type="text" @click="onOpenAddCrawl">新增</el-button>
						<el-button size="small" type="text" @click="onOpenEditCrawl(scope.row)">修改</el-button>
						<el-button size="small" type="text" @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
    <AddCrawl ref="addCrawlRef"/>
    <EditCrawl ref="editCrawlRef"/>
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted, defineComponent } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import AddCrawl from '/@/views/crawl/component/addCrawl.vue';
import EditCrawl from '/@/views/crawl/component/editCrawl.vue';

// 定义接口来定义对象的类型
interface TableDataRow {
	crawlName: string;
	createTime: string;
	status: boolean;
	sort: number;
	describe: string;
	id: number;
	children?: TableDataRow[];
}
interface TableDataState {
	tableData: {
		data: Array<TableDataRow>;
		total: number;
		loading: boolean;
		param: {
			pageNum: number;
			pageSize: number;
		};
	};
}

export default defineComponent({
	name: 'dataCrawl',
	components: { AddCrawl, EditCrawl },
	setup() {
		const addCrawlRef = ref();
		const editCrawlRef = ref();
		const state = reactive<TableDataState>({
			tableData: {
				data: [],
				total: 0,
				loading: false,
				param: {
					pageNum: 1,
					pageSize: 10,
				},
			},
		});
		// 初始化表格数据
		const initTableData = () => {
			state.tableData.data.push({
				crawlName: '景点',
				createTime: new Date().toLocaleString(),
				status: true,
				sort: Math.random(),
				describe: '景点数据爬虫',
				id: Math.random(),
				children: [
					{
            crawlName: '美团',
						createTime: new Date().toLocaleString(),
						status: true,
						sort: Math.random(),
						describe: '美团景点',
						id: Math.random(),
					},
					{
            crawlName: '去哪儿',
						createTime: new Date().toLocaleString(),
						status: true,
						sort: Math.random(),
						describe: '去哪儿景点',
						id: Math.random(),
					},
				],
			});
      state.tableData.data.push({
        crawlName: '酒店',
        createTime: new Date().toLocaleString(),
        status: true,
        sort: Math.random(),
        describe: '酒店数据爬虫',
        id: Math.random(),
        children: [
          {
            crawlName: '美团',
            createTime: new Date().toLocaleString(),
            status: true,
            sort: Math.random(),
            describe: '美团酒店',
            id: Math.random(),
          },
          {
            crawlName: '携程',
            createTime: new Date().toLocaleString(),
            status: true,
            sort: Math.random(),
            describe: '携程酒店',
            id: Math.random(),
          },
        ],
      });
			state.tableData.total = state.tableData.data.length;
		};
		// 打开新增菜单弹窗
		const onOpenAddCrawl = () => {
			addCrawlRef.value.openDialog();
		};
		// 打开编辑菜单弹窗
		const onOpenEditCrawl = (row: TableDataRow) => {
      editCrawlRef.value.openDialog(row);
		};
		// 删除当前行
		const onTabelRowDel = (row: TableDataRow) => {
			ElMessageBox.confirm(`此操作将永久删除爬虫规则：${row.deptName}, 是否继续?`, '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					ElMessage.success('删除成功');
				})
				.catch(() => {});
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		return {
      addCrawlRef,
      editCrawlRef,
			onOpenAddCrawl,
			onOpenEditCrawl,
			onTabelRowDel,
			...toRefs(state),
		};
	},
});
</script>
